package com.msh.artascope.devcloud.service.service.impl;


import com.msh.artascope.devcloud.client.po.EnvPo;
import com.msh.artascope.devcloud.service.service.EnvService;
import com.msh.artascope.devcloud.service.dao.KubernetesConfigDao;
import com.msh.artascope.devcloud.service.model.query.KubernetesConfigQueryVo;
import com.msh.artascope.devcloud.service.model.vo.KubernetesConfigVo;
import com.msh.artascope.devcloud.service.wrapper.K8sWrapper.K8sInfo;
import com.msh.frame.client.base.TenantBaseServiceImpl;
import com.msh.frame.client.exception.BizException;
import com.msh.frame.common.util.StringUtil;
import javax.annotation.Resource;

import com.msh.frame.interfaces.IdGenerateable;
import com.msh.artascope.devcloud.client.po.KubernetesConfigPo;
import com.msh.artascope.devcloud.client.qo.KubernetesConfigQo;
import com.msh.artascope.devcloud.service.service.KubernetesConfigService;
import com.msh.frame.client.common.Result;
import java.util.stream.Collectors;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.*;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

/**
 * @author shihu
 * @mail m-sh@qq.com
 */
@Service
@Slf4j
public class KubernetesConfigServiceImpl extends TenantBaseServiceImpl<KubernetesConfigPo,KubernetesConfigQo> implements KubernetesConfigService {

  @Resource
  private EnvService envService;

  @Resource
  private KubernetesConfigDao kubernetesConfigDao;

  @Resource
  private IdGenerateable idGenerateable;

  @Override
  @Transactional(rollbackFor = Exception.class)
  public long insert(KubernetesConfigPo param) {
    param.setId(idGenerateable.getUniqueId());
    return super.insert(param);
  }

  @Override
  public int insertCollection(Collection<KubernetesConfigPo> param) {
    param.stream().forEach(p->p.setId(idGenerateable.getUniqueId()));
    return super.insertCollection(param);
  }


  @Override
  public KubernetesConfigPo getByEnvId(Long envId, Long tenantId) {
    return listOne(
        new KubernetesConfigQo().setEnvId(envId).setTenantId(tenantId));
  }

  @Override
  public K8sInfo getK8s(Long envId, Long tenantId) {
    EnvPo envPo = envService.get(envId, tenantId);
    BizException.doThrowIfNull("无法获取环境信息", envPo);
    return getK8s(envPo, tenantId);
  }

  @Override
  public K8sInfo getK8s(EnvPo envPo, Long tenantId){
    KubernetesConfigPo kubernetesConfigPo = listOne(
            new KubernetesConfigQo().setEnvId(envPo.getId()).setTenantId(tenantId));
    BizException.doThrowIfNull("无法获取k8s信息", kubernetesConfigPo);
    return new K8sInfo(kubernetesConfigPo.getAddress(), kubernetesConfigPo.getToken(), envPo.getNamespace());
  }

  @Override
  public void update(KubernetesConfigPo param) {
    Long id = param.getId();
    Long tenantId = param.getTenantId();
    KubernetesConfigPo po = this.get(id, tenantId);
    String tokenDb = po.getToken();
    String desensitize = StringUtil.desensitize(tokenDb, 20, 0);
    if(null != desensitize && desensitize.length()>60){
      desensitize = desensitize.substring(0, 60);
    }
    if(desensitize.equals(param.getToken()) || StringUtils.isEmpty(param.getToken())){
      param.setToken(null);
    }
    super.update(param);
  }

  // ----------前端vo处理-------------------------------------------------------

  private KubernetesConfigVo toVo(KubernetesConfigPo po){
    KubernetesConfigVo vo = po.convert(KubernetesConfigVo.class);
    String desensitize = StringUtil.desensitize(vo.getToken(), 20, 0);
    if(null != desensitize && desensitize.length()>60){
      desensitize = desensitize.substring(0, 60);
    }
    vo.setToken(desensitize);
    return vo;
  }

  private List<KubernetesConfigVo> toVoList(List<KubernetesConfigPo> list){
    if(CollectionUtils.isEmpty(list)){
      return Collections.emptyList();
    }

    return list.stream().map(po->{
      return toVo(po);
    }).collect(Collectors.toList());
  }

  @Override
  public KubernetesConfigVo getVo(Long id, Long tenantId) {
    KubernetesConfigPo po = this.get(id, tenantId);
    if(null == po){
      return null;
    }
    KubernetesConfigVo vo = toVo(po);

    return vo;
  }

  @Override
  public List<KubernetesConfigVo> listVo(KubernetesConfigQueryVo q){
    KubernetesConfigQo qo = q.convert(KubernetesConfigQo.class);
    List<KubernetesConfigPo> poList = this.list(qo);
    List<KubernetesConfigVo> voList = toVoList(poList);

    return voList;
  }

  @Override
  public Result<List<KubernetesConfigVo>> pageVo(KubernetesConfigQueryVo param) {
    KubernetesConfigQo qo = param.convert(KubernetesConfigQo.class);
    int count = this.count(qo);
    if (0 == count) {
      return Result.successReturn(Collections.<KubernetesConfigVo>emptyList()).setCount(count);
    }
    List<KubernetesConfigPo> poList = this.list(qo);
    List<KubernetesConfigVo> voList = toVoList(poList);

    return Result.successReturn(voList).setCount(count);
  }


}
